package com.service;
import static com.def.GenericDef.ACTION_FLG_ADD;
import static com.def.GenericDef.ACTION_FLG_CANCEL;
import static com.def.GenericDef.ACTION_FLG_DELETE;
import static com.def.GenericDef.ACTION_FLG_INQUIRE;
import static com.def.GenericDef.ACTION_FLG_NEW;
import static com.def.GenericDef.ACTION_FLG_QUERY;
import static com.def.GenericDef.ACTION_FLG_UPDATE;
import static com.def.GenericDef.E_XPAPLYDN_OTHER_ERROR;
import static com.def.GenericDef.E_XPMLITEM_M_ITEM_NOT_FOUND;
import static com.def.GenericDef.E_XPMLITEM_M_LABEL_ALREADY_EXIST;
import static com.def.GenericDef.E_XPMLITEM_NOT_FOUND_M_LABEL;
import static com.def.GenericDef.RETURN_CODE_OK;
import static com.def.GenericDef.RETURN_CODE_UNKNOWN;
import static com.def.GenericDef.RETURN_MESG_OK;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.log4j.Logger;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.dao.IBisDataDao;
import com.dao.IBisMItemDao;
import com.dao.IBisMLabelDao;
import com.dao.IBisToolDao;
import com.dao.IBisToolGrDao;
import com.dao.IRetToolPmDao;
import com.dao.IRetWODao;
import com.dao.ISpcRetMfgDataDao;
import com.dao.impl.BisMtrlHibernateDao;
import com.icim.service.ICIMBaseService;
import com.model.Bis_data;
import com.model.Bis_m_item;
import com.model.Bis_m_itemId;
import com.model.Bis_m_label;
import com.model.Bis_m_labelId;
import com.model.Bis_tool_gr;
import com.model.Ret_tool_pm;
import com.model.Ret_tool_pmId;
import com.model.Ret_wo;
import com.model.Spc_ret_mfg_data;
import com.model.Spc_ret_mfg_dataId;
import com.tx.Xpcmsrdt.XpcmsrdtIA;
import com.tx.Xpmlitem.XpmlitemI;
import com.tx.Xpmlitem.XpmlitemIA;
import com.tx.Xpmlitem.XpmlitemIB;
import com.tx.Xpmlitem.XpmlitemO;
import com.tx.Xpmlitem.XpmlitemOA;
import com.tx.Xpmlitem.XpmlitemOB;
import com.tx.Xpmlitem.XpmlitemOC;
import com.util.GUIDGenerator;
import com.util.GUIDGenerator16;
import com.util.MqOpe;
import com.util.Util;

@Scope("prototype")
@Service("XpmlitemService")
public class XpmlitemService extends ICIMBaseService{
	private static Logger logger = Logger.getLogger(XpmlitemService.class);
	@Autowired
	private XpmlitemI inTrx;
	
	@Autowired
	private XpmlitemO outTrx; 
	
	@Autowired
	private IRetWODao retWODao;
	
	@Autowired
	private IBisMLabelDao bisMLabelDao;
	
	@Autowired
	private IBisMItemDao bisMItemDao;
	
	@Autowired
	private IBisDataDao bisDataDao;
	@Autowired
	private IBisToolGrDao bisToolGrDao;
	@Autowired 
	private IBisToolDao bisToolDao;
	@Autowired
	ISpcRetMfgDataDao spcRetMfgDataDao;
	@Autowired
	private MqOpe mqOpe;
	
	@Autowired
	private IRetToolPmDao retToolPmDao;
	public String subMainProc(String strInMsg){
		logger.info("InTrx is "+ strInMsg );
		
		outTrx.setTrx_id("XPMLITEM");
		outTrx.setType_id("O");
		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg("success");
		try{
			Session session = bisMLabelDao.getCrSession();
			inTrx = (XpmlitemI) mqOpe.inStr2Obj(strInMsg,inTrx);
			char action_flg = inTrx.getAction_flg().charAt(0);
			switch( action_flg ){
				case ACTION_FLG_QUERY   :  queryByCondition()   ; break;   
				case ACTION_FLG_ADD     :  addMLabelFnc()       ; break;
				case ACTION_FLG_UPDATE  :  updateMLabelFnc()    ; break;
				case ACTION_FLG_DELETE  :  deleteMLabelFnc()    ; break;
				case ACTION_FLG_INQUIRE :  queryMItemFnc()      ; break;
				case ACTION_FLG_NEW     :  addORUpdateMItemFnc(); break;
//				case ACTION_FLG_REPLACE :  updatePathItemFnc() ; break;
				case ACTION_FLG_CANCEL  :  deleteMItemFnc()     ; break;
				case 'B'                :  queryforMesId()      ; break;
				case 'S'                :  savePM()             ; break;
				case 'G'                :  getPM()              ; break;
				case 'E'                :  deletepm()           ; break;
				case 'F'                :  regpmvalue()         ; break;
				case 'H'                :  getpmdata()          ; break;
				case 'M'				:  getMesIdByEqpt()	    ; break;
				default                 :  errActionFnc();
			}
			if(RETURN_CODE_OK.equals(outTrx.getRtn_code())){
				session.flush();
			}else{
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			}
		}catch(Exception ex){
			outTrx.setRtn_code(RETURN_CODE_UNKNOWN);
			if(!("").equals(ex.getCause().toString())){
				outTrx.setRtn_mesg(ex.getCause().toString());
			}else{
				outTrx.setRtn_mesg(ex.toString());
			}
			logger.error(Util.stackTraceToString(ex));
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
		}finally{
			logger.info(mqOpe.obj2Str(outTrx));
		}
		return mqOpe.obj2Str(outTrx);
	}
	private void getMesIdByEqpt() {
		
		List<XpmlitemIA> iaryAList = inTrx.getIaryA();
		if(iaryAList == null ){
			return;
		}
		if(iaryAList.isEmpty()){
			return;
		}
		XpmlitemIA iaryA = iaryAList.get(0);
		String toolId = iaryA.getTool_id_fk();
		String hql = "FROM Bis_m_item where tool_id_fk = ?";
		List<Bis_m_item> bisMItemList = bisMItemDao.list(hql, toolId);
		if(bisMItemList.isEmpty()){
			return;
		}
		Set<String> mesIdSet = new HashSet<String>();
		
		List<XpmlitemOA> oaryAList = new ArrayList<XpmlitemOA>();
		for (Bis_m_item bisMItem : bisMItemList) {
			mesIdSet.add(bisMItem.getId().getMes_id_fk());
		}
		for (String mesId : mesIdSet) {
			XpmlitemOA oarya  = new XpmlitemOA();
			oarya.setMes_id_fk(mesId);
			oaryAList.add(oarya);
		}
	
		outTrx.setOaryA(oaryAList);
		outTrx.setTbl_cnt_a(oaryAList.size());
	}
	public void queryByCondition(){
		if(inTrx.getIaryA()==null){
			return;
		}
		XpmlitemIA iaryA = inTrx.getIaryA().get(0);
		List<XpmlitemOA> oaryAList = new ArrayList<XpmlitemOA>();
		List<XpmlitemOB> oaryBList = new ArrayList<XpmlitemOB>();
		String tool_id_fk   = iaryA.getTool_id_fk();
		String rep_unit     = iaryA.getRep_unit();
		String data_pat     = iaryA.getData_pat();
		String mes_id_fk    = iaryA.getMes_id_fk();
		String ope_no       = iaryA.getOpe_no();
		StringBuffer baseBuf = new StringBuffer();
		StringBuffer toolItemBuffer = new StringBuffer("FROM Bis_m_item where 1=1");
		StringBuffer toolGItemBuffer = new StringBuffer("FROM Bis_m_item where 1=1");
		String label_hql = "FROM Bis_m_label where 1=1";
		String order_hql = " order by  cast(id.data_id as integer)";
//		String item_hql  = "FROM Bis_m_item where 1=1";
		if(tool_id_fk!=null){
			if(tool_id_fk.indexOf('*')==-1){
				label_hql = label_hql + " and id.tool_id_fk='" + tool_id_fk + "'";
				toolItemBuffer.append(" and id.tool_id_fk='").append(tool_id_fk).append("'");
			}else{
				label_hql = label_hql + " and id.tool_id_fk like '" + tool_id_fk.replace('*', '%') + "'";
			}
		}
		if(rep_unit!=null){
			if(rep_unit.indexOf('*')==-1){
				label_hql = label_hql + " and id.rep_unit='" + rep_unit + "'";
				baseBuf.append(" and id.rep_unit_fk='").append(rep_unit).append("'");
			}else{
				label_hql = label_hql + " and id.rep_unit like '" + rep_unit.replace('*', '%') + "'";
			}
		}
		if(data_pat!=null){
			if(data_pat.indexOf('*')==-1){
				label_hql = label_hql + " and id.data_pat='" + data_pat + "'";
				baseBuf.append(" and id.data_pat_fk='").append(data_pat).append("'");
			}else{
				label_hql = label_hql + " and id.data_pat like '" + data_pat.replace('*', '%') + "'";
			}
		}
		
		if(mes_id_fk!=null){
			if(mes_id_fk.indexOf('*')==-1){
				label_hql = label_hql + " and id.mes_id_fk='" + mes_id_fk + "'";
				baseBuf.append(" and id.mes_id_fk='").append(mes_id_fk).append("'");
			}else{
				label_hql = label_hql + " and id.mes_id_fk like '" + mes_id_fk.replace('*', '%') + "'";
			}
		}
		
		List<Bis_m_label> bisMLabelList = bisMLabelDao.find(label_hql); 
		for(Bis_m_label bis_m_label : bisMLabelList ){
			XpmlitemOA oaryA = new XpmlitemOA();
			oaryA.setTool_id_fk(bis_m_label.getId().getTool_id_fk());
			oaryA.setRep_unit(bis_m_label.getId().getRep_unit());
			oaryA.setData_pat(bis_m_label.getId().getData_pat());
			String hql2 = "FROM Bis_data where data_cate='MLDP' and data_id='"+
			bis_m_label.getId().getData_pat()+"'";
			List<Bis_data> bisDataList = bisDataDao.find(hql2);
			if(bisDataList.size()!=0){//TODO:find 返回的数组，当结果集为空，如何判断
				Bis_data bis_data = bisDataList.get(0);
				oaryA.setData_pat_txt(bis_data.getData_ext());
			}
			oaryA.setMes_id_fk(bis_m_label.getId().getMes_id_fk());
			oaryA.setOpi_data_flg(bis_m_label.getOpi_data_flg());
			oaryA.setEvt_user(bis_m_label.getEvt_user());
			oaryAList.add(oaryA);
		}
		toolItemBuffer.append(baseBuf).append(order_hql);
		
		List<Bis_m_item> bisMItemList = bisMItemDao.find(toolItemBuffer.toString()); 
		if(bisMItemList.isEmpty()){
			String toolHql = "FROM Bis_tool_gr where tool_id_fk = ? ";
			Bis_tool_gr bisTool =(Bis_tool_gr)bisToolGrDao.uniqueResult(toolHql, tool_id_fk);;
			
//			String toolGHql = "FROM Bis_tool_gr where toolg_id = ?";
//			List<Bis_tool_gr> bisToolGrList = bisToolGrDao.list(toolGHql, bisTool.getId().getToolg_id());
//			toolGItemBuffer.append(" and tool_id_fk in(");
//			if(bisToolGrList.isEmpty()){
//				return;
//			}
//			for (Bis_tool_gr bisToolGr : bisToolGrList) {
//				toolGItemBuffer.append("'").append(bisToolGr.getId().getTool_id_fk()).append("'");
//				if (bisToolGrList.indexOf(bisToolGr) != bisToolGrList.size() - 1) {
//					toolGItemBuffer.append(",");
//				}
//			}
//			toolGItemBuffer.append(")");
			toolGItemBuffer.append("and tool_id_fk='").append(bisTool.getId().getToolg_id()).append("'");
			toolGItemBuffer.append(baseBuf).append(order_hql);
			bisMItemList = bisMItemDao.find(toolGItemBuffer.toString());
			
			if(bisMItemList.isEmpty()){
				if ( inTrx.getQ_mode() != null && !inTrx.getQ_mode().equals('1') ){
					outTrx.setRtn_code(RETURN_CODE_UNKNOWN);
					outTrx.setRtn_mesg("找不到机台量测参数,请去6A00界面维护");
					return ;
				}
			}
		}
		
		String hql = "FROM Bis_data where data_cate = ? and data_ext = ? and data_item = ?";
		List<Bis_data> bisDataList = bisDataDao.list(hql, "SPEC", inTrx.getWo_id_fk(), ope_no);
//		bisDataList.get(0);
//		Bis_data bisData = bisDataDao.uniqueResult(hql, "SPEC","123");
		String upper = "";
		String lower = "";
		boolean spec_flg = false;
		if(!bisDataList.isEmpty()){
			Bis_data bis_data = bisDataList.get(0);
			if("CDPC".equals(iaryA.getMes_id_fk())){
				upper = bis_data.getExt_1();
				lower = bis_data.getExt_2();
				spec_flg = true;
			}else if ("CZPC".equals(iaryA.getMes_id_fk())){
				upper = bis_data.getExt_3();
				lower = bis_data.getExt_4();
				spec_flg = true;
			}
		}
		double d_fthick = 0;
		double d_tthick = 0;
		if(inTrx.getWo_id_fk() != null){
			String wo_sql = "FROM Ret_wo where wo_typ='W' and wo_id = '"+inTrx.getWo_id_fk()+"'";
			List<Ret_wo> woList = retWODao.find(wo_sql);
			if (woList != null) {
				for (Ret_wo ret_wo : woList) {
		               d_fthick = ret_wo.getFrom_thickness();
     	               d_tthick = ret_wo.getTo_thickness();
				}
			
		    }
		}
		
		
		for(Bis_m_item bis_m_item : bisMItemList ){
			XpmlitemOB oaryB = new XpmlitemOB();
			oaryB.setTool_id_fk(bis_m_item.getId().getTool_id_fk());
			oaryB.setRep_unit_fk(bis_m_item.getId().getRep_unit_fk());
			oaryB.setData_pat_fk(bis_m_item.getId().getData_pat_fk());
			oaryB.setMes_id_fk(bis_m_item.getId().getMes_id_fk());
			oaryB.setOpi_data_flg(bis_m_item.getOpi_data_flg());
			oaryB.setData_id(bis_m_item.getId().getData_id());
			oaryB.setData_group(bis_m_item.getData_group());
			oaryB.setData_dsc(bis_m_item.getData_dsc());
			oaryB.setData_group_seq(bis_m_item.getData_group_seq());
			oaryB.setData_attr(bis_m_item.getData_attr());
			oaryB.setSpec_chk_flg(bis_m_item.getSpec_chk_flg());
			oaryB.setSpc_mode(bis_m_item.getSpc_mode());
			oaryB.setSpec_chk_typ(bis_m_item.getSpec_chk_typ());
			if (bis_m_item.getSpec_chk_typ() != null && !bis_m_item.getSpec_chk_typ().equals("0")){
				if(bis_m_item.getSpec_chk_typ().equals("1")){
					if(bis_m_item.getU_spec()!=null){
						oaryB.setU_spec(String.valueOf(d_fthick+bis_m_item.getU_spec().doubleValue()));//
					}
					if(bis_m_item.getL_spec()!=null){
						oaryB.setL_spec(String.valueOf(d_fthick+bis_m_item.getL_spec().doubleValue()));//
					}
					
				}else if(bis_m_item.getSpec_chk_typ().equals("2")){
					if(bis_m_item.getU_spec()!=null){
						oaryB.setU_spec(String.valueOf(d_tthick+bis_m_item.getU_spec().doubleValue()));//
					}
					if(bis_m_item.getL_spec()!=null){
						oaryB.setL_spec(String.valueOf(d_tthick+bis_m_item.getL_spec().doubleValue()));//
					}							
				}
			}else{				
				if(bis_m_item.getU_spec()!=null){
					oaryB.setU_spec(bis_m_item.getU_spec().toString());//
				}
				if(bis_m_item.getL_spec()!=null){
					oaryB.setL_spec(bis_m_item.getL_spec().toString());//
				}
			}
			if (spec_flg){
				oaryB.setU_spec(upper);
				oaryB.setL_spec(lower);
			}
			oaryBList.add(oaryB);
		}
		outTrx.setOaryA(oaryAList);
		outTrx.setTbl_cnt_a(oaryAList.size());
		outTrx.setOaryB(oaryBList);
		outTrx.setTbl_cnt_b(oaryBList.size());
	}
	public void addMLabelFnc() {
		XpmlitemIA iaryA = inTrx.getIaryA().get(0);
		String tool_id_fk   = iaryA.getTool_id_fk();
		String rep_unit     = iaryA.getRep_unit();
		String data_pat     = iaryA.getData_pat();
		String mes_id_fk    = iaryA.getMes_id_fk();
		Bis_m_labelId id = new Bis_m_labelId();
		id.setTool_id_fk(tool_id_fk);
		id.setRep_unit(rep_unit);
		id.setData_pat(data_pat);
		id.setMes_id_fk(mes_id_fk);
		Bis_m_label query_bis_m_label = bisMLabelDao.get(id); 
		if(query_bis_m_label!=null){
			outTrx.setRtn_code(E_XPMLITEM_M_LABEL_ALREADY_EXIST+"");
			outTrx.setRtn_mesg("record already exist");
			return ;
		}
		Bis_m_label bis_m_label = new Bis_m_label();
		bis_m_label.setId(id);
		bis_m_label.setOpi_data_flg(iaryA.getOpi_data_flg());
		bis_m_label.setEvt_user(iaryA.getEvt_user());
		bis_m_label.setEvt_timestamp(Util.getCurrentTimestamp());
		bisMLabelDao.save(bis_m_label);
	}
	public void updateMLabelFnc(){
		XpmlitemIA iaryA = inTrx.getIaryA().get(0);
		String tool_id_fk   = iaryA.getTool_id_fk();
		String rep_unit     = iaryA.getRep_unit();
		String data_pat     = iaryA.getData_pat();
		String mes_id_fk    = iaryA.getMes_id_fk();
		Bis_m_labelId id = new Bis_m_labelId();
		id.setTool_id_fk(tool_id_fk);
		id.setRep_unit(rep_unit);
		id.setData_pat(data_pat);
		id.setMes_id_fk(mes_id_fk);
		Bis_m_label bis_m_label = bisMLabelDao.get(id); 
		if( bis_m_label!=null){
			outTrx.setRtn_code(E_XPMLITEM_NOT_FOUND_M_LABEL+"");
			outTrx.setRtn_mesg("not found record");
			return ;
		}
		if(iaryA.getOpi_data_flg()!=null){
			bis_m_label.setOpi_data_flg(iaryA.getOpi_data_flg());	
		}
		if(iaryA.getEvt_user()!=null){
			bis_m_label.setEvt_user(iaryA.getEvt_user());
		}
		if(iaryA.getEvt_user()!=null){
			bis_m_label.setEvt_user(iaryA.getEvt_user());
		}
		
		bis_m_label.setEvt_timestamp(Util.getCurrentTimestamp());
		bisMLabelDao.update(bis_m_label);
	}
	public void deleteMLabelFnc(){
		XpmlitemIA iaryA = inTrx.getIaryA().get(0);
		String tool_id_fk   = iaryA.getTool_id_fk();
		String rep_unit     = iaryA.getRep_unit();
		String data_pat     = iaryA.getData_pat();
		String mes_id_fk    = iaryA.getMes_id_fk();
		Bis_m_labelId id = new Bis_m_labelId();
		id.setTool_id_fk(tool_id_fk);
		id.setRep_unit(rep_unit);
		id.setData_pat(data_pat);
		id.setMes_id_fk(mes_id_fk);
		Bis_m_label bis_m_label = bisMLabelDao.get(id); 
		if( bis_m_label==null){
			outTrx.setRtn_code(E_XPMLITEM_NOT_FOUND_M_LABEL+"");
			outTrx.setRtn_mesg("not found record");
			return ;
		}
		bisMLabelDao.delete(bis_m_label);
		//将Measure item 信息删除
		String hql = "FROM Bis_m_item where id.tool_id_fk='"+ tool_id_fk +"'"
		           + " and id.rep_unit_fk ='"  + rep_unit  +"'"
		           + " and id.data_pat_fk ='"  + data_pat  +"'"
		           + " and id.mes_id_fk ='" + mes_id_fk +"'";
		List<Bis_m_item> mItemList = bisMItemDao.find(hql);
		for(Bis_m_item bis_m_item:mItemList){
			bisMItemDao.delete(bis_m_item);
		}
	}
	public void queryMItemFnc(){
		//同时查询出MLabel信息
		queryByCondition();
		XpmlitemIB iaryB = inTrx.getIaryB().get(0);
		List<XpmlitemOB> oaryBList = new ArrayList<XpmlitemOB>();
		String tool_id_fk      = iaryB.getTool_id_fk();
		String rep_unit_fk     = iaryB.getRep_unit_fk();
		String data_pat_fk     = iaryB.getData_pat_fk();
		String mes_id_fk       = iaryB.getMes_id_fk();
		String data_id         = iaryB.getData_id();
		String data_group      = iaryB.getData_group();
		String hql = "FROM Bis_m_item where 1=1";
		if( tool_id_fk != null){
			if(tool_id_fk.indexOf('*')==-1){
				hql = hql + " and id.tool_id_fk='" + tool_id_fk + "'";
			}else{
				hql = hql + " and id.tool_id_fk like '" + tool_id_fk.replace('*', '%') + "'";
			}
		}
		if(rep_unit_fk!=null){
			if(rep_unit_fk.indexOf('*')==-1){
				hql = hql + " and id.rep_unit_fk='" + rep_unit_fk + "'";
			}else{
				hql = hql + " and id.rep_unit_fk like '" + rep_unit_fk.replace('*', '%') + "'";
			}
		}
		if(data_pat_fk!=null){
			if(data_pat_fk.indexOf('*')==-1){
				hql = hql + " and id.data_pat_fk='" + data_pat_fk + "'";
			}else{
				hql = hql + " and id.data_pat_fk like '" + data_pat_fk.replace('*', '%') + "'";
			}
		}
		if(mes_id_fk!=null){
			if(mes_id_fk.indexOf('*')==-1){
				hql = hql + " and id.mes_id_fk='" + mes_id_fk + "'";
			}else{
				hql = hql + " and id.mes_id_fk like '" + mes_id_fk.replace('*', '%') + "'";
			}
		}
		if(data_id!=null){
			if(data_id.indexOf('*')==-1){
				hql = hql + " and id.data_id='" + data_id + "'";
			}else{
				hql = hql + " and id.data_id like '" + data_id.replace('*', '%') + "'";
			}
		}
		if(data_group!=null){
			hql = hql + " and data_id = '" + data_id + "'";
		}
		List<Bis_m_item> bisMItemList = bisMItemDao.find(hql);
		for(Bis_m_item bis_m_item:bisMItemList){
			XpmlitemOB oaryB = new XpmlitemOB();
			oaryB.setTool_id_fk(bis_m_item.getId().getTool_id_fk());
			oaryB.setRep_unit_fk(bis_m_item.getId().getRep_unit_fk());
			oaryB.setData_pat_fk(bis_m_item.getId().getData_pat_fk());
			oaryB.setMes_id_fk(bis_m_item.getId().getMes_id_fk());
			oaryB.setData_id(bis_m_item.getId().getData_id());
			oaryB.setData_group(bis_m_item.getData_group());
			oaryB.setData_attr(bis_m_item.getData_attr());
			oaryB.setData_dsc(bis_m_item.getData_dsc());
			oaryB.setData_seq(bis_m_item.getData_seq());
            oaryB.setData_group_seq(bis_m_item.getData_group_seq());
            oaryB.setUse_eap_lbl_flg(bis_m_item.getUse_eap_lbl_flg());
            oaryB.setOpi_data_flg(bis_m_item.getOpi_data_flg());
            oaryB.setSpec_chk_flg(bis_m_item.getSpec_chk_flg());
            oaryB.setSpc_mode(bis_m_item.getSpc_mode());
            oaryB.setSum_group_flg(bis_m_item.getSum_group_flg());
            oaryB.setSum_group_cnt(bis_m_item.getSum_group_cnt());
            oaryB.setSpec_chk_typ(bis_m_item.getSpec_chk_typ());
            if(bis_m_item.getL_spec()!=null){
            	oaryB.setL_spec(bis_m_item.getL_spec().toString());
            }
            if(bis_m_item.getU_spec()!=null){
            	oaryB.setU_spec(bis_m_item.getU_spec().toString());
            }
            oaryB.setEvt_usr(bis_m_item.getEvt_usr());
			oaryBList.add(oaryB);
		}
		outTrx.setOaryB(oaryBList);
		outTrx.setTbl_cnt_b(oaryBList.size());
		
	}
	public void addORUpdateMItemFnc(){
		//如果不存在measure label,新insert一笔
		List<XpmlitemIB> iaryBList = inTrx.getIaryB();
		String query_tool_id="";
		String query_rep_unit="";
		String query_data_pat="";
		String query_mes_id="";
		if( iaryBList != null ){
			XpmlitemIB iaryB = iaryBList.get(0);
			query_tool_id = iaryB.getTool_id_fk();
			query_rep_unit = iaryB.getRep_unit_fk();
			query_data_pat = iaryB.getData_pat_fk();
			query_mes_id   = iaryB.getMes_id_fk();
			
			Bis_m_labelId labelID = new Bis_m_labelId();
			labelID.setTool_id_fk(query_tool_id);
			labelID.setRep_unit(query_rep_unit);
			labelID.setData_pat(query_data_pat);
			labelID.setMes_id_fk(query_mes_id);
			Bis_m_label bis_m_label = bisMLabelDao.get(labelID);
			if(bis_m_label==null){
				bis_m_label = new Bis_m_label();
				bis_m_label.setId(labelID);
				bis_m_label.setEvt_user(iaryB.getEvt_usr());
				bis_m_label.setEvt_timestamp(Util.getCurrentTimestamp());
				bisMLabelDao.save(bis_m_label);
			}
		}
		//删除bis_m_item表中所有记录
		String hql = "FROM Bis_m_item where id.tool_id_fk ='"+ query_tool_id +"'" +
		           " and id.rep_unit_fk ='" + query_rep_unit + "'" +
		           " and id.data_pat_fk ='" + query_data_pat + "'" +
		           " and id.mes_id_fk ='" + query_mes_id +"'";
		List<Bis_m_item> bisMItemList = bisMItemDao.find(hql);
		for(Bis_m_item bis_m_item:bisMItemList){
			bisMItemDao.delete(bis_m_item);
		}
		//insert 重新insert
		for(XpmlitemIB iaryB:iaryBList){
			String tool_id_fk      = iaryB.getTool_id_fk();
			String rep_unit_fk     = iaryB.getRep_unit_fk();
			String data_pat_fk     = iaryB.getData_pat_fk();
			String mes_id_fk       = iaryB.getMes_id_fk();
			String data_id         = iaryB.getData_id();
			Bis_m_itemId id = new Bis_m_itemId();
			id.setTool_id_fk(tool_id_fk);
			id.setRep_unit_fk(rep_unit_fk);
			id.setData_pat_fk(data_pat_fk);
			id.setMes_id_fk(mes_id_fk);
			id.setData_id(data_id);
			
			Bis_m_item bis_m_item = bisMItemDao.get(id);
			if( bis_m_item == null ){
				bis_m_item = new Bis_m_item();
				bis_m_item.setId(id);
			}
			if(iaryB.getData_attr()!=null){
				bis_m_item.setData_attr(iaryB.getData_attr());
			}
            if(iaryB.getData_dsc()!=null){
            	bis_m_item.setData_dsc(iaryB.getData_dsc());
            }
            if(iaryB.getData_seq()!=null){
            	bis_m_item.setData_seq(iaryB.getData_seq());
            }
            if(iaryB.getData_group()!=null){
            	bis_m_item.setData_group(iaryB.getData_group());
            }
            if(iaryB.getData_group_seq()!=null){
            	bis_m_item.setData_group_seq(iaryB.getData_group_seq());
            }
            if(iaryB.getUse_eap_lbl_flg()!=null){
            	bis_m_item.setUse_eap_lbl_flg(iaryB.getUse_eap_lbl_flg());
            }
            if(iaryB.getOpi_data_flg()!=null){
            	bis_m_item.setOpi_data_flg(iaryB.getOpi_data_flg());
            }
            if(iaryB.getSpec_chk_flg()!=null){
            	bis_m_item.setSpec_chk_flg(iaryB.getSpec_chk_flg());
            }
            if(iaryB.getSpc_mode()!=null){
            	bis_m_item.setSpc_mode(iaryB.getSpc_mode());
            }
            if(iaryB.getSum_group_flg()!=null){
            	bis_m_item.setSum_group_flg(iaryB.getSum_group_flg());
            }
            if(iaryB.getSum_group_cnt()!=null){
            	bis_m_item.setSum_group_cnt(iaryB.getSum_group_cnt());
            }
            if(iaryB.getSpec_chk_typ()!=null){
            	bis_m_item.setSpec_chk_typ(iaryB.getSpec_chk_typ());
            }
            if(Util.isSpaceCheck(iaryB.getL_spec())== false){
            	bis_m_item.setL_spec(Double.valueOf(iaryB.getL_spec()));	
            }
            if(Util.isSpaceCheck(iaryB.getU_spec())== false){
            	bis_m_item.setU_spec(Double.valueOf(iaryB.getU_spec()));
            }
            if(iaryB.getEvt_usr()!=null){
            	bis_m_item.setEvt_usr(iaryB.getEvt_usr());
            }
            bis_m_item.setEvt_timestamp(Util.getCurrentTimestamp());
	        bisMItemDao.save(bis_m_item);
		}
		
		
		
	}    
	public void updateMItemFnc(){
		//删除route信息  重写insert
		List<XpmlitemIB> iaryBList = inTrx.getIaryB();
		for(XpmlitemIB iaryB:iaryBList){
			String tool_id_fk      = iaryB.getTool_id_fk();
			String rep_unit_fk     = iaryB.getRep_unit_fk();
			String data_pat_fk     = iaryB.getData_pat_fk();
			String mes_id_fk       = iaryB.getMes_id_fk();
			String data_id         = iaryB.getData_id();
			Bis_m_itemId id = new Bis_m_itemId();
			id.setTool_id_fk(tool_id_fk);
			id.setRep_unit_fk(rep_unit_fk);
			id.setData_pat_fk(data_pat_fk);
			id.setMes_id_fk(mes_id_fk);
			id.setData_id(data_id);
			
			Bis_m_item bis_m_item = bisMItemDao.get(id);
			if( bis_m_item == null ){
				outTrx.setRtn_code(E_XPMLITEM_M_ITEM_NOT_FOUND+"");
				outTrx.setRtn_mesg("not found record");
				return;
			}
			if(iaryB.getData_attr()!=null){
				bis_m_item.setData_attr(iaryB.getData_attr());
			}
            if(iaryB.getData_dsc()!=null){
            	bis_m_item.setData_dsc(iaryB.getData_dsc());
            }
            if(iaryB.getData_seq()!=null){
            	bis_m_item.setData_seq(iaryB.getData_seq());
            }
            if(iaryB.getData_group()!=null){
            	bis_m_item.setData_group(iaryB.getData_group());
            }
            if(iaryB.getData_group_seq()!=null){
            	bis_m_item.setData_group_seq(iaryB.getData_group_seq());
            }
            if(iaryB.getUse_eap_lbl_flg()!=null){
            	bis_m_item.setUse_eap_lbl_flg(iaryB.getUse_eap_lbl_flg());
            }
            if(iaryB.getOpi_data_flg()!=null){
            	bis_m_item.setOpi_data_flg(iaryB.getOpi_data_flg());
            }
            if(iaryB.getSpec_chk_flg()!=null){
            	bis_m_item.setSpec_chk_flg(iaryB.getSpec_chk_flg());
            }
            if(iaryB.getSpc_mode()!=null){
            	bis_m_item.setSpc_mode(iaryB.getSpc_mode());
            }
            if(iaryB.getSum_group_flg()!=null){
            	bis_m_item.setSum_group_flg(iaryB.getSum_group_flg());
            }
            if(iaryB.getSum_group_cnt()!=null){
            	bis_m_item.setSum_group_cnt(iaryB.getSum_group_cnt());
            }
            if(iaryB.getSpec_chk_typ()!=null){
            	bis_m_item.setSpec_chk_typ(iaryB.getSpec_chk_typ());
            }
            if(Util.isSpaceCheck(iaryB.getL_spec())==false){
            	bis_m_item.setL_spec(Double.valueOf(iaryB.getL_spec()));	
            }
            if(Util.isSpaceCheck(iaryB.getU_spec())==false){
            	bis_m_item.setL_spec(Double.valueOf(iaryB.getU_spec()));	
            }
            if(iaryB.getEvt_usr()!=null){
            	bis_m_item.setEvt_usr(iaryB.getEvt_usr());
            }
            bis_m_item.setEvt_timestamp(Util.getCurrentTimestamp());
	        bisMItemDao.save(bis_m_item);
		}
		
	}
	public void deleteMItemFnc(){
		List<XpmlitemIB> iaryBList = inTrx.getIaryB();
		for(XpmlitemIB iaryB:iaryBList){
			String tool_id_fk      = iaryB.getTool_id_fk();
			String rep_unit_fk     = iaryB.getRep_unit_fk();
			String data_pat_fk     = iaryB.getData_pat_fk();
			String mes_id_fk       = iaryB.getMes_id_fk();
			String data_id         = iaryB.getData_id();
			Bis_m_itemId id = new Bis_m_itemId();
			id.setTool_id_fk(tool_id_fk);
			id.setRep_unit_fk(rep_unit_fk);
			id.setData_pat_fk(data_pat_fk);
			id.setMes_id_fk(mes_id_fk);
			id.setData_id(data_id);
			
			Bis_m_item bis_m_item = bisMItemDao.get(id);
			if(bis_m_item==null){
				outTrx.setRtn_code(E_XPMLITEM_M_ITEM_NOT_FOUND+"");
				outTrx.setRtn_mesg("not found record");
				return ;
			}
			bisMItemDao.delete(bis_m_item);
		}
        
	}
	public void errActionFnc(){
		outTrx.setTrx_id("XPMLITEM");
		outTrx.setType_id("O");
		outTrx.setRtn_code("0000009");
		outTrx.setRtn_mesg("Invalid Action_flg");
	}
    public void testFnc(){
    	BisMtrlHibernateDao testDao = new BisMtrlHibernateDao();
    	testDao.getHibernateTemplate().getSessionFactory().getCurrentSession();
    	Session session = bisMItemDao.getCrSession();
    	Transaction tx = session.beginTransaction();
    	tx.commit();
    	
    }	
    
	public void queryforMesId(){

		XpmlitemIA iaryA = inTrx.getIaryA().get(0);
		List<XpmlitemOA> oaryAList = new ArrayList<XpmlitemOA>();
		String tool_id_fk   = iaryA.getTool_id_fk();
        String label_hql = "FROM Bis_m_label where tool_id_fk='" + tool_id_fk + "' AND data_pat='02'";
        
	    List<Bis_m_label> bisMLabelList = bisMLabelDao.find(label_hql); 
		for(Bis_m_label bis_m_label : bisMLabelList ){
			XpmlitemOA oaryA = new XpmlitemOA();
			oaryA.setTool_id_fk(bis_m_label.getId().getTool_id_fk());
			oaryA.setMes_id_fk(bis_m_label.getId().getMes_id_fk());
			oaryAList.add(oaryA);
		}
		outTrx.setOaryA(oaryAList);
		outTrx.setTbl_cnt_a(oaryAList.size());
	}
	
	public void savePM(){
		XpmlitemIA iaryA = inTrx.getIaryA().get(0);
		String tool_id = iaryA.getTool_id_fk();
		String pm_type = iaryA.getPm_type();
		Ret_tool_pmId id = new Ret_tool_pmId();
		id.setPm_type(pm_type);
		id.setTool_id_fk(tool_id);
		Ret_tool_pm ret_tool_pm = retToolPmDao.get(id);
		if(ret_tool_pm!=null){
			outTrx.setRtn_code(E_XPAPLYDN_OTHER_ERROR+"");
			outTrx.setRtn_mesg("此机台已经设置此类保养，请勿重复添加");
			return;
		}else{
			Ret_tool_pm ret_tool_pm_new = new Ret_tool_pm();
			ret_tool_pm_new.setId(id);
			Date fromdate = java.sql.Date.valueOf(iaryA.getFrom_date());
			ret_tool_pm_new.setFrom_date(fromdate);
			Date todate = java.sql.Date.valueOf(iaryA.getTo_date());
			ret_tool_pm_new.setTo_date(todate);
			ret_tool_pm_new.setMes_id_fk(iaryA.getMes_id_fk());
			ret_tool_pm_new.setMes_id_fk(iaryA.getMes_id_fk());
			ret_tool_pm_new.setClm_usr(iaryA.getEvt_user());
			ret_tool_pm_new.setClm_timestamp(Util.getCurrentTimestamp());
			retToolPmDao.save(ret_tool_pm_new);
			outTrx.setRtn_code( RETURN_CODE_OK );
			outTrx.setRtn_mesg( RETURN_MESG_OK );
		}
	}
	
	public void getPM() throws ParseException{

		XpmlitemIA iaryA = inTrx.getIaryA().get(0);
		List<XpmlitemOA> oaryAList = new ArrayList<XpmlitemOA>();
		List<XpmlitemOC> oaryCList = new ArrayList<XpmlitemOC>();
		String tool_id_fk   = iaryA.getTool_id_fk();
        String hql = "FROM Ret_tool_pm where tool_id_fk='" + tool_id_fk + "'";
        
	    List<Ret_tool_pm> toolpmlist = retToolPmDao.find(hql); 
		for(Ret_tool_pm ret_tool_pm : toolpmlist ){
			XpmlitemOA oaryA = new XpmlitemOA();
			oaryA.setTool_id_fk(ret_tool_pm.getId().getTool_id_fk());
			oaryA.setMes_id_fk(ret_tool_pm.getMes_id_fk());
			String hqldsc = "FROM Bis_data where data_cate=? and data_id=?";
			Bis_data bis_data = bisDataDao.uniqueResult(hqldsc, "MLID",ret_tool_pm.getMes_id_fk());
			if(bis_data.getData_desc()!=null){
				oaryA.setMes_dsc(bis_data.getData_desc());
			}else{
				oaryA.setMes_dsc(ret_tool_pm.getMes_id_fk());
			}
			SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");  
			String fromdate=sdf.format(ret_tool_pm.getFrom_date()); 
			String todate=sdf.format(ret_tool_pm.getTo_date()); 
			oaryA.setFrom_date(fromdate.toString());
			oaryA.setTo_date(todate.toString());
			oaryA.setPm_type(ret_tool_pm.getId().getPm_type());
			String hqltypedsc = "FROM Bis_data where data_cate=? and data_id=?";
			Bis_data bis_data1 = bisDataDao.uniqueResult(hqltypedsc, "PMTP",ret_tool_pm.getId().getPm_type());
			if(null!=bis_data1){
				oaryA.setPm_type_dsc(bis_data1.getData_ext());
			}else{
				oaryA.setPm_type_dsc(ret_tool_pm.getId().getPm_type());
			}
			oaryA.setPm_seq_id(ret_tool_pm.getPm_seq_id());
			oaryAList.add(oaryA);
		}
		outTrx.setOaryC(oaryCList);
		outTrx.setOaryA(oaryAList);
		outTrx.setTbl_cnt_a(oaryAList.size());
	}
	
	public void deletepm(){
		XpmlitemIA iaryA = inTrx.getIaryA().get(0);
		String tool_id = iaryA.getTool_id_fk();
		String pm_type = iaryA.getPm_type();
		Ret_tool_pmId id = new Ret_tool_pmId();
		id.setPm_type(pm_type);
		id.setTool_id_fk(tool_id);
		Ret_tool_pm ret_tool_pm = retToolPmDao.get(id);
		if(ret_tool_pm!=null){
			retToolPmDao.delete(ret_tool_pm);
			outTrx.setRtn_code( RETURN_CODE_OK );
			outTrx.setRtn_mesg( RETURN_MESG_OK );
		}else{
			outTrx.setRtn_code(E_XPAPLYDN_OTHER_ERROR+"");
			outTrx.setRtn_mesg("要删除的数据不存在，请确认");
			return;
		}
	}
	
	private void regpmvalue(){
			Ret_tool_pmId id = new Ret_tool_pmId();
			id.setPm_type(inTrx.getPm_type());
			id.setTool_id_fk(inTrx.getTool_id_fk());
			
			Ret_tool_pm ret_tool_pm = retToolPmDao.get(id);
			ret_tool_pm.setAc_pm_beg_timestamp(inTrx.getAc_pm_beg_timestamp());
			ret_tool_pm.setAc_pm_end_timestamp(inTrx.getAc_pm_end_timestamp());
			ret_tool_pm.setClm_timestamp(Util.getCurrentTimestamp());
			ret_tool_pm.setClm_usr(inTrx.getEvt_user());
			ret_tool_pm.setPm_seq_id(Util.getRandomString(16));
			ret_tool_pm.setAc_pm_note(inTrx.getAc_pm_note());
			retToolPmDao.update(ret_tool_pm);
			
			int data_cnt =Integer.valueOf( inTrx.getData_cnt() );
			List<XpmlitemIB> iaryList =inTrx.getIaryB();
			int data_seq_cnt = data_cnt/30 +1;
			int data_index = 0;
			int MAX_DATA_VALUE_CNT = 30;
			
			for(int i=0;i<data_seq_cnt;i++){
				Spc_ret_mfg_data spc_ret_mfg_data = new Spc_ret_mfg_data();
				Spc_ret_mfg_dataId idm = new Spc_ret_mfg_dataId();
				idm.setData_seq_id_fk(i+1);
				idm.setEvt_seq_id_fk(GUIDGenerator16.javaGUID());
				idm.setPrd_seq_id_fk(ret_tool_pm.getPm_seq_id());
				spc_ret_mfg_data.setId(idm);
				spc_ret_mfg_data.setUnq_seq_id(GUIDGenerator.javaGUID());
				spc_ret_mfg_data.setTool_id_fk(inTrx.getTool_id_fk());
				spc_ret_mfg_data.setMes_id_fk(ret_tool_pm.getMes_id_fk());
				spc_ret_mfg_data.setRpt_usr(inTrx.getEvt_user());
				spc_ret_mfg_data.setRpt_timestamp(Util.getCurrentTimestamp());
				spc_ret_mfg_data.setData_cnt(data_cnt);
				
				for(int j=0;j<MAX_DATA_VALUE_CNT;j++){
					data_index = i*MAX_DATA_VALUE_CNT + j;
					if(data_index==data_cnt){
						break;
					}
					String data_value =  iaryList.get(data_index).getData_value() ;
					if(j==0){
						spc_ret_mfg_data.setData_value_001(data_value);
					}else if(j==1){
						spc_ret_mfg_data.setData_value_002(data_value);
					}else if(j==2){
						spc_ret_mfg_data.setData_value_003(data_value);
					}else if(j==3){
						spc_ret_mfg_data.setData_value_004(data_value);
					}else if(j==4){
						spc_ret_mfg_data.setData_value_005(data_value);
					}else if(j==5){
						spc_ret_mfg_data.setData_value_006(data_value);
					}else if(j==6){
						spc_ret_mfg_data.setData_value_007(data_value);
					}else if(j==7){
						spc_ret_mfg_data.setData_value_008(data_value);
					}else if(j==8){
						spc_ret_mfg_data.setData_value_009(data_value);
					}else if(j==9){
						spc_ret_mfg_data.setData_value_010(data_value);
					}else if(j==10){
						spc_ret_mfg_data.setData_value_011(data_value);
					}else if(j==11){
						spc_ret_mfg_data.setData_value_012(data_value);
					}else if(j==12){
						spc_ret_mfg_data.setData_value_013(data_value);
					}else if(j==13){
						spc_ret_mfg_data.setData_value_014(data_value);
					}else if(j==14){
						spc_ret_mfg_data.setData_value_015(data_value);
					}else if(j==15){
						spc_ret_mfg_data.setData_value_016(data_value);
					}else if(j==16){
						spc_ret_mfg_data.setData_value_017(data_value);
					}else if(j==17){
						spc_ret_mfg_data.setData_value_018(data_value);
					}else if(j==18){
						spc_ret_mfg_data.setData_value_019(data_value);
					}else if(j==19){
						spc_ret_mfg_data.setData_value_020(data_value);
					}else if(j==20){
						spc_ret_mfg_data.setData_value_021(data_value);
					}else if(j==21){
						spc_ret_mfg_data.setData_value_022(data_value);
					}else if(j==22){
						spc_ret_mfg_data.setData_value_023(data_value);
					}else if(j==23){
						spc_ret_mfg_data.setData_value_024(data_value);
					}else if(j==24){
						spc_ret_mfg_data.setData_value_025(data_value);
					}else if(j==25){
						spc_ret_mfg_data.setData_value_026(data_value);
					}else if(j==26){
						spc_ret_mfg_data.setData_value_027(data_value);
					}else if(j==27){
						spc_ret_mfg_data.setData_value_028(data_value);
					}else if(j==28){
						spc_ret_mfg_data.setData_value_029(data_value);
					}else if(j==29){
						spc_ret_mfg_data.setData_value_030(data_value);
					}
				}
				spcRetMfgDataDao.save(spc_ret_mfg_data);
			}
	}
	
	public void getpmdata() throws ParseException{

		XpmlitemIA iaryA = inTrx.getIaryA().get(0);
		List<XpmlitemOA> oaryAList = new ArrayList<XpmlitemOA>();
		List<XpmlitemOB> oaryBList = new ArrayList<XpmlitemOB>();
		List<XpmlitemOC> oaryCList = new ArrayList<XpmlitemOC>();
        
		String tool_id = iaryA.getTool_id_fk();
		String pm_type = iaryA.getPm_type();
		Ret_tool_pmId id = new Ret_tool_pmId();
		id.setPm_type(pm_type);
		id.setTool_id_fk(tool_id);
		Ret_tool_pm ret_tool_pm = retToolPmDao.get(id);
		
		if(ret_tool_pm==null){
			outTrx.setRtn_code(E_XPAPLYDN_OTHER_ERROR+"");
			outTrx.setRtn_mesg("此机台未设置此类保养");
			return;
		}else{
			XpmlitemOA oaryA = new XpmlitemOA();
			oaryA.setPm_type(ret_tool_pm.getId().getPm_type());
			String hqltypedsc = "FROM Bis_data where data_cate=? and data_id=?";
			Bis_data bis_data1 = bisDataDao.uniqueResult(hqltypedsc, "PMTP",ret_tool_pm.getId().getPm_type());
			if(null!=bis_data1){
				oaryA.setPm_type_dsc(bis_data1.getData_ext());
			}else{
				oaryA.setPm_type_dsc(ret_tool_pm.getId().getPm_type());
			}
			//oaryb
	        String hql = "FROM Bis_m_item where tool_id_fk='" + ret_tool_pm.getId().getTool_id_fk() +
	        		     "' and data_pat_fk='02' and mes_id_fk='" + ret_tool_pm.getMes_id_fk() + "'";
	        List<Bis_m_item> itemlist = bisMItemDao.find(hql);
	        if(itemlist.size()==0){
				outTrx.setRtn_code(E_XPAPLYDN_OTHER_ERROR+"");
				outTrx.setRtn_mesg("此机台未设置此类保养项");
				return;
	        }
			for(Bis_m_item bis_m_item:itemlist){
				XpmlitemOB oaryB = new XpmlitemOB();
				oaryB.setTool_id_fk(bis_m_item.getId().getTool_id_fk());
				oaryB.setRep_unit_fk(bis_m_item.getId().getRep_unit_fk());
				oaryB.setData_pat_fk(bis_m_item.getId().getData_pat_fk());
				oaryB.setMes_id_fk(bis_m_item.getId().getMes_id_fk());
				oaryB.setOpi_data_flg(bis_m_item.getOpi_data_flg());
				oaryB.setData_id(bis_m_item.getId().getData_id());
				oaryB.setData_group(bis_m_item.getData_group());
				oaryB.setData_dsc(bis_m_item.getData_dsc());
				oaryB.setData_group_seq(bis_m_item.getData_group_seq());
				oaryB.setData_attr(bis_m_item.getData_attr());
				oaryB.setSpec_chk_flg(bis_m_item.getSpec_chk_flg());
				oaryB.setSpc_mode(bis_m_item.getSpc_mode());
				oaryBList.add(oaryB);
				}
			
			//oaryb
			//oaryc
			SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");  
			String fromdate=sdf.format(ret_tool_pm.getFrom_date()); 
			String todate=sdf.format(ret_tool_pm.getTo_date()); 
			Calendar calendar = Calendar.getInstance();
			Date begdate = sdf.parse(fromdate);
			Date enddate = sdf.parse(todate);
	        calendar.setTime(begdate);
	        if(ret_tool_pm.getId().getPm_type().equals("PM01")){
	        	for(int i=0;begdate.compareTo(enddate)<=0;i++){
	        		XpmlitemOC oaryC = new XpmlitemOC();
	        		oaryC.setPm_date(sdf.format(begdate));
	        		oaryC.setPm_type(ret_tool_pm.getId().getPm_type());
	        		oaryC.setPm_type_dsc(oaryA.getPm_type_dsc());
	        		calendar.add(Calendar.DATE,+7);
	        		begdate = calendar.getTime();
	        		oaryCList.add(oaryC);
	        	}
	        }
	        if(ret_tool_pm.getId().getPm_type().equals("PM02")){
	        	for(int i=0;begdate.compareTo(enddate)<=0;i++){
	        		XpmlitemOC oaryC = new XpmlitemOC();
	        		oaryC.setPm_date(sdf.format(begdate));
	        		oaryC.setPm_type(ret_tool_pm.getId().getPm_type());
	        		oaryC.setPm_type_dsc(oaryA.getPm_type_dsc());
	        		calendar.add(Calendar.MONTH,+1);
	        		begdate = calendar.getTime();
	        		oaryCList.add(oaryC);
	        	}
	        }
	        if(ret_tool_pm.getId().getPm_type().equals("PM03")){
	        	for(int i=0;begdate.compareTo(enddate)<=0;i++){
	        		XpmlitemOC oaryC = new XpmlitemOC();
	        		oaryC.setPm_date(sdf.format(begdate));
	        		oaryC.setPm_type(ret_tool_pm.getId().getPm_type());
	        		oaryC.setPm_type_dsc(oaryA.getPm_type_dsc());
	        		calendar.add(Calendar.MONTH,+3);
	        		begdate = calendar.getTime();
	        		oaryCList.add(oaryC);
	        	}
	        }
			//end oary3
		outTrx.setOaryC(oaryCList);
		outTrx.setOaryA(oaryAList);
		outTrx.setTbl_cnt_a(oaryAList.size());
		outTrx.setOaryB(oaryBList);
		outTrx.setTbl_cnt_b(oaryBList.size());
		}
        

	}
}
